package com.taotao.search.service.impl;

import java.util.List;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;

import com.taotao.common.pojo.SearchItem;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.search.mapper.SearchItemMapper;
import com.taotao.search.service.SearchItemService;

public class SearchItemServiceImpl implements SearchItemService {
	
	@Autowired
	private SearchItemMapper itemMapper;
	@Autowired
	private SolrServer server;

	@Override
	public TaotaoResult importItemIndex() {
			try{
				List<SearchItem> list=itemMapper.getItemList();
				for (SearchItem searchItem : list) {
					SolrInputDocument document=new SolrInputDocument();
					document.addField("id", searchItem.getId());
					document.addField("title", searchItem.getTitle());
					document.addField("sell_point", searchItem.getSell_point());
					document.addField("price", searchItem.getPrice());
					document.addField("image", searchItem.getImage());
					document.addField("category_name", searchItem.getCategory_name());
					document.addField("item_desc", searchItem.getItem_desc());
					server.add(document);
				}
				server.commit();
			}catch(Exception e){
				e.printStackTrace();
				return TaotaoResult.build(500, "数据倒入失败");
			}
		return TaotaoResult.ok();
	}

}
